" vim: ft=vim
" Test the int() converter.

Execute(single int);
for i in range(-10, 10)
  AssertEqual object#int(i), i
endfor


Execute(single float);
let input = [
      \ [0.0, 0],
      \ [1.2, 1],
      \ [-1.2, -1],
      \ [1.5, 1],
      \ [-1.5, -1],
      \]

for [in, out] in input
  AssertEqual object#int(in), out
endfor


Execute(single string);
for i in range(-10, 10)
  AssertEqual object#int(string(i)), i
endfor


Execute(String with base 10);
let input = [
      \ '  22',
      \ '22  ',
      \ '  22  ',
      \ '22',
      \ '+22',
      \ '-22',
      \]
for x in input
  AssertEqual object#int(x, 10), str2nr(x)
  AssertEqual object#int(x), str2nr(x)
endfor


Execute(String with base 2);
let input = [
      \ ' 0b101',
      \ '0b101 ',
      \ '  0b101 ',
      \ '0b101',
      \ '0B101',
      \ '101',
      \ '-101',
      \ '-0b101',
      \ '-0B101',
      \ '+101',
      \ '+0b101',
      \ '+0B101',
      \]
for x in input
  AssertEqual object#int(x, 2), str2nr(x, 2)
endfor

Execute(String with base 8);
let input = [
      \ '071',
      \ '+071',
      \ '-071',
      \ '71',
      \ '-71',
      \ '+71',
      \]
for x in input
  AssertEqual object#int(x, 8), str2nr(x, 8)
endfor


Execute(String with base 16);
let input = [
      \ '  0X123ABC',
      \ '0X123ABC  ',
      \ '  0X123ABC  ',
      \ '0X123ABC',
      \ '+0X123ABC',
      \ '-0X123ABC',
      \ '0x123abc',
      \ '+0x123abc',
      \ '-0x123abc',
      \ '+123abc',
      \ '-123abc',
      \ '123abc',
      \]
for x in input
  AssertEqual object#int(x, 16), str2nr(x, 16)
endfor

" Wrong base.
Execute(Throws bad base);
for bad_base in range(3, 5)
  AssertThrows call object#int('1', g:bad_base)
  Assert g:vader_exception =~# 'ValueError'
endfor

" Throws invalid literal
" Regex Checks.
Execute(letters in decimal);
AssertThrows call object#int('12a')
Assert g:vader_exception =~# 'ValueError'

Execute(Bad placement of space);
AssertThrows call object#int('- 12')
Assert g:vader_exception =~# 'ValueError'

AssertThrows call object#int(' - 0b10', 2)
Assert g:vader_exception =~# 'ValueError'

AssertThrows call object#int('- 0b 10', 2)
Assert g:vader_exception =~# 'ValueError'

Execute(A string without any digits fails);
AssertThrows call object#int('')
Assert g:vader_exception =~# 'ValueError'

" Logical Error.
Execute(Explicit base without String literal);
AssertThrows call object#int(1.2, 2)
Log g:vader_exception
Assert g:vader_exception =~# 'TypeError'

Execute(Find no way to convert);
AssertThrows call object#int({})
Log g:vader_exception
Assert g:vader_exception =~# 'TypeError'

Execute(Hook into __int__);
let cls = {}
function! cls.__int__()
  return 1
endfunction

AssertEqual object#int(cls), 1
